/*
 * Copyright 2009 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.gwt.baselib.ui.client.menu;

import java.util.List;
import java.util.Vector;

import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * The panel showing the menuitems.<br/>
 * Do not instantiate this panel directly, but use the {@link MenuPanelManager}
 * instead.
 * 
 * @author Joost A. Bloemsma
 */
public class MenuPanel extends FlowPanel implements IMenuPanel {
	private final Vector<IMenuItem> menuItems = new Vector<IMenuItem>();
	private final IMenuPanelManager manager;
	
	/**
	 * Constructs a new MenuPanel instance
	 * 
	 * @param manager
	 */
	public MenuPanel(IMenuPanelManager manager) {
		this.manager = manager;
	}
	
	/**
	 * Do not use!<br/>
	 * Will throw an IllegalAccessError stating to use the
	 * {@link #addMenuItem(IMenuItem)} method.
	 */
	@Override
	public void add(Widget w) {
		throw new RuntimeException("Use addMenuItem to add items to the menu");
	}
	
	/**
	 * Adds a new widget to the menu
	 * 
	 * @param w
	 * @param selected
	 */
	private void add(Widget w, boolean selected) {
		if (w == null)
			return;
		if (selected) {
			w.setStyleName("menuItem_selected");
		} else {
			w.setStyleName("menuItem");
		}
		super.add(w);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void addMenuItems(List<IMenuItem> items) {
		if (items == null)
			return;
		for (IMenuItem item : items) {
			this.addMenuItem(item);
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void addMenuItem(IMenuItem menuItem) {
		if (menuItem == null)
			return;
		menuItem.setManager(this.manager);
		menuItems.add(menuItem);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void removeMenuItems(List<IMenuItem> items) {
		if (items == null)
			return;
		menuItems.removeAll(items);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void removeMenuItem(IMenuItem menuItem) {
		if (menuItem == null)
			return;
		menuItems.remove(menuItem);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void rebuildMenu() {
		this.clear();
		boolean loggedIn = this.manager.isLoggedIn();
		for (IMenuItem mi : menuItems) {
			if (mi.isAlwaysVisible() || loggedIn) {
				this.add(mi.getWidget(), mi.isSelected());
			}
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setMenuItemSelected(IMenuItem item, boolean selected) {
		if (selected) {
			item.getWidget().setStyleName("menuItem_selected");
		} else {
			item.getWidget().setStyleName("menuItem");
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void setOnlySelectedMenuItem(IMenuItem item) {
		for (IMenuItem mi : menuItems) {
			setMenuItemSelected(mi, (mi == item));
		}
	}
	
}
